<template>
  <div>
    <el-card>
      <el-form :inline="true">
        <el-form-item label="一级分类">
          <el-select
            :disabled="scene"
            v-model="categoryStore.c1Id"
            @change="handler1"
          >
            <!-- label 显示的文字 value 收集的数据-->
            <el-option
              v-for="c1 in categoryStore.c1Arr"
              :key="c1.id"
              :label="c1.name"
              :value="c1.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select
            :disabled="scene"
            v-model="categoryStore.c2Id"
            @change="handler2"
          >
            <el-option
              v-for="c2 in categoryStore.c2Arr"
              :key="c2.id"
              :label="c2.name"
              :value="c2.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="三级分类">
          <el-select :disabled="scene" v-model="categoryStore.c3Id">
            <el-option
              v-for="c3 in categoryStore.c3Arr"
              :key="c3.id"
              :label="c3.name"
              :value="c3.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from "vue";
// 引入分类小仓库
import useCategoryStore from "@/store/modules/category";
const categoryStore = useCategoryStore();

// 挂载完毕的时候,发送请求
onMounted(() => {
  getC1();
});

// 获取一级分类
const getC1 = () => {
  // 通知仓库发送请求
  categoryStore.getC1();
};

// 一级分类值改变的回调
const handler1 = () => {
  // 清空二级分类数据
  categoryStore.c2Id = "";
  // 清空三级分类数据
  categoryStore.c3Id = "";
  // 通知仓库获取二级分类
  categoryStore.getC2();
};

// 二级分类值改变的回调
const handler2 = () => {
  // 清空三级分类数据
  categoryStore.c3Id = "";
  // 通知仓库获取二级分类
  categoryStore.getC3();
};

// 接受父组件传递的scene
defineProps(["scene"]);
</script>

<style scoped>
.el-form-item {
  width: 400px;
}
</style>
